package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.YonbipFileUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 人员基础信息Controller
 * 
 * @author ruoyi
 * @date 2025-09-08
 */
@Controller
@RequestMapping("/system/psndoc")
public class BdPsndocController extends BaseController
{
    private String prefix = "system/psndoc";

    @Autowired
    private IBdPsndocService bdPsndocService = SpringUtils.getBean(IBdPsndocService.class);;

    private IOrgDeptService orgDeptService = SpringUtils.getBean(IOrgDeptService.class);
    private IBdPsnjobService bdPsnjobService = SpringUtils.getBean(IBdPsnjobService.class);
    private IOrgAccbookService orgAccbookService=SpringUtils.getBean(IOrgAccbookService.class);

    @RequiresPermissions("system:psndoc:view")
    @GetMapping()
    public String psndoc()
    {
        return prefix + "/psndoc";
    }

    /**
     * 查询人员基础信息列表
     */
    @RequiresPermissions("system:psndoc:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BdPsndoc bdPsndoc)
    {
        startPage();
        List<BdPsndoc> list = bdPsndocService.selectBdPsndocList(bdPsndoc);
        return getDataTable(list);
    }

    /**
     * 导出人员基础信息列表
     */
    @RequiresPermissions("system:psndoc:export")
    @Log(title = "人员基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BdPsndoc bdPsndoc)
    {
        List<BdPsndoc> list = bdPsndocService.selectBdPsndocList(bdPsndoc);
        ExcelUtil<BdPsndoc> util = new ExcelUtil<BdPsndoc>(BdPsndoc.class);
        return util.exportExcel(list, "人员基础信息数据");
    }

    /**
     * 新增人员基础信息
     */
    @RequiresPermissions("system:psndoc:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存人员基础信息
     */
    @RequiresPermissions("system:psndoc:add")
    @Log(title = "人员基础信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BdPsndoc bdPsndoc)
    {
        return toAjax(bdPsndocService.insertBdPsndoc(bdPsndoc));
    }

    /**
     * 修改人员基础信息
     */
    @RequiresPermissions("system:psndoc:edit")
    @GetMapping("/edit/{pkPsndoc}")
    public String edit(@PathVariable("pkPsndoc") String pkPsndoc, ModelMap mmap)
    {
        BdPsndoc bdPsndoc = bdPsndocService.selectBdPsndocByPkPsndoc(pkPsndoc);
        mmap.put("bdPsndoc", bdPsndoc);
        return prefix + "/edit";
    }

    /**
     * 修改保存人员基础信息
     */
    @RequiresPermissions("system:psndoc:edit")
    @Log(title = "人员基础信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BdPsndoc bdPsndoc)
    {
        return toAjax(bdPsndocService.updateBdPsndoc(bdPsndoc));
    }

    /**
     * 删除人员基础信息
     */
    @RequiresPermissions("system:psndoc:remove")
    @Log(title = "人员基础信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bdPsndocService.deleteBdPsndocByPkPsndocs(ids));
    }

    public void ApiQueryPsn(){


        try
        {

            List<OrgAccbook> orgAccbooklist= orgAccbookService.selectOrgAccbookList(new OrgAccbook());

            for(OrgAccbook orgAccbook :orgAccbooklist){
                List<String> lines=new ArrayList<>();
                List<String> linesSb=new ArrayList<>();
                //成功数
                int cnum=0;
                //失败数
                int snum=0;
                BdPsndoc bdPsndoc=new BdPsndoc();
                bdPsndoc.setPkOrg(orgAccbook.getPkRelorg());
                List<BdPsndoc> list = bdPsndocService.selectBdPsndocList(bdPsndoc);

                for(BdPsndoc bdPsndoc1: list){
                    BdPsnjob bdPsnjob=bdPsnjobService.selectBdPsnjobByPkPsnjob(bdPsndoc1.getPkPsndoc());
                   if(StringUtils.isNotNull(bdPsnjob)){
                       cnum++;
                       lines.add(bdPsnjob.getPSNCODE()+"|"+bdPsndoc1.getNAME()+"|"+bdPsnjob.getPkOrg()+"|"+bdPsnjob.getPkDept()
                               +"|"+bdPsndoc1.getMOBILE()+"|"+bdPsnjob.getPkPsncl()+"|"+bdPsnjob.getINDUTYDATE()+"|"+bdPsnjob.getENDDUTYDATE()) ;
                   }else{
                       snum++;
                   }

                }
                YonbipFileUtils.createFileWithLines(
                        "C:/basedata",
                        DateUtil.format(DateUtil.date(),"yyyyMMdd")+"-"+orgAccbook.getCODE()+"-person.txt",
                        lines,
                        false
                );

                linesSb.add("人员数据导入成功"+cnum+"条");
                linesSb.add("人员数据导入失败"+snum+"条");
                YonbipFileUtils.createFileWithLines(
                        "C:/basedata",
                        DateUtil.format(DateUtil.date(),"yyyyMMdd")+"-"+orgAccbook.getCODE()+"-person.txt.ok",
                        linesSb,
                        false
                );

            }


        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
